<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <div id="app">
        <div>{{myComputedName}}</div>
        <div>{{computedName}}</div>
        <div>{{writableComputedName}}</div>
    </div>
    <script>
        var obj = {
            data() {
                return {
                    myname: 'zhj',
                    year: 2054,
                    month: 12,
                    day: 12
                }
            },
            computed: {
                myComputedName() {
                    console.log('计算属性被调用了');
                    return this.myname.substring(0, 1).toUpperCase() + this.myname.substring(1);
                },
                computedName() {
                    return this.year + "-" + this.month + "-" + this.day;
                },
                // 计算属性默认是不可写，不可修改的.要想可以修改，
                // 需要设置为可写，写成对象形式
                writableComputedName: {
                    get() {
                        return this.year + "-" + this.month + "-" + this.day
                    },
                    set(value) {
                        console.log(value.split('-'));
                        this.year = value.split('-')[0];
                        this.month = value.split('-')[1];
                        this.day = value.split('-')[2];
                        console.log(this.year, this.month, this.day);
                        // 也可以这样写：
                        // [this.year, this.month, this.day] = value.split('-');
                        // console.log(this.year, this.month, this.day);
                    }
                }

            }
        }
        var app = Vue.createApp(obj).mount('#app')

        // 计算属性默认是不可写，不可修改的
        // app.myComputedName = 'zs';
        // app.computedName = '2023-12-12';

        // 计算属性可以设置为可写，但是不建议这样做
        // app.$options.computed.myComputedName.writable = true;
        // app.myComputedName = 'zs';
        // app.$options.computed.computedName.writable = true;
        // app.computedName = '2023-12-12';
        // console.log(app);
        // console.log(app.myComputedName);
        // console.log(app.computedName);
        // 计算属性默认是懒加载的，也就是第一次使用的时候才会计算，如果想要在创建的时候就计算可以使用watcher
        // app.myComputedName;
        // app.computedName;
    </script>
</body>

</html>